
<% content_for :head do %>
<script type="text/javascript" charset="utf-8">

var popHealth = popHealth || {};

popHealth.initSorting = function() {
  $('#patientTable >thead >tr > th').click(function(){
     if ($(this).attr('name') != sort){
       sort=$(this).attr('name');
     }
     else {
       sort_ascending = !sort_ascending;
     }
     $('#patientTableContainer').html("<div style='padding: 150px;'><img src='/assets/loading_bar.gif'/></div>");
     popHealth.reloadTable();
  });
}

popHealth.initPagination = function() {
  $(".pagination > a").click(function(){  
    var url = this.href;
    this.href="#";
    $.ajax({ url:  url,
               type:"GET",
               dataType: 'html',
               success: function(res){
                 $('#patientTableContainer').html(res);
               },
               error: function(xhr, err) {
                   alert("There was an error handling the request for the page.  Please try again.");
               }
             });
    return false;
  });
}

popHealth.initExclusions = function() {
  $( ".exclude" ).button().click(function() {
      $( "#dialog-form" ).data("link", $(this).data("link")).dialog("option", "title", $(this).data().title).dialog("open");
  });
  
  var rationale = $( "#rationale" );
  
  $( "#dialog-form" ).dialog({
    autoOpen: false,
    height: 215,
    width: 410,
    modal: true,
    buttons: {
      Update: function() {
          $('#patientTableContainer').html("<div style='padding: 150px;'><img src='/assets/loading_bar.gif'/></div>");
          $.ajaxSetup({async:false});
          $.post(
            $(this).data("link"),
            {rationale: rationale.val()},
            function(data) {
              popHealth.poll();
            }
          );

          $( this ).dialog( "close" );
        },
      Cancel: function() {
        $( this ).dialog( "close" );
      }
    },
    close: function() {
      rationale.val("");
    }
  });

  
}

$(document).ready(function() {
  popHealth.initExclusions();
  popHealth.initSorting();
  popHealth.initPagination();
});

</script>
<% end -%>


 <table id="patientTable">
    <thead>
       <tr>
         <th name="medical_record_id">Patient ID</th>
         <th name="last">Last</th>
         <th name="first">First</th>
         <th name="birthdate">Age</th>
         <th name="birthdate">DoB</th>
         <th name="gender">Sex</th>
         <th name="excluded">Exclusion</th>
         <% if (@type == 'exclusions') %>
           <th name="manual_exclusion_rationale">Rationale</th>
         <% end %>
       </tr>
     </thead>
     <tbody>
     <% @page_results.each do |patient_container| -%>
       <tr class="antiNumerator">
         <% patient = patient_container['value'] -%>
         <td><%= link_to (patient['medical_record_id'] || 'UNK'), url_for(controller: :patients, action: :show, id: patient_container['value']['patient_id']) %></td>         <td><%= patient['last'] %></td>
         <td><%= patient['first'] %></td>
         <td><%= age_from_time(patient['birthdate']) || 'UNK' %></td>
         <td><%= dob(patient['birthdate']) || 'UNK' %></td>
         <td><%= patient["gender"] || "UNK" %></td>

         <td class="button">
           <%= patient['manual_exclusion'] ? "Excluded [#{patient['manual_exclusion_rationale']}]" : 'Included' %>
           <button class="exclude" 
               data-link="<%= url_for(controller: :patients, action: :toggle_excluded, id: patient['medical_record_id'], measure_id: patient['measure_id'], sub_id: patient['sub_id']) %>"
               data-title="<%= patient['manual_exclusion'] ? 'Include Patient' : 'Exclude Patient' %>">
             Change
           </button>

         </td>
         <% if (@type == 'exclusions') %>
           <td>
             <% if patient['manual_exclusion'] 
                 exclusion = @manual_exclusions[patient['medical_record_id']] if @manual_exclusions[patient['medical_record_id']]
             %>
               <span title="<%= exclusion.rationale %> (excluded by: <%= exclusion.user.full_name%> on <%= exclusion.created_at.strftime("%m/%d/%Y") %>)" class="vtip">Manual: <%= truncate(exclusion.rationale, :length => 30)%></span>
             <% else %>
               <span title="The patient was automatically excluded based on the exclusion logic of the measure." class="vtip">Automatic</span>
             <% end %>
           </td>
         <% end %>
       </tr>
     <% end -%>
     </tbody>
  </table>
<div>
  <!-- pagination to go here -->
  <%= will_paginate @page_results%>
</div>



<div id="dialog-form" title="Exclude Patient">
	<form onsubmit="return false;">
  	<fieldset>
		  <label for="rationale">Rationale: </label>
		  <textarea name="rationale" cols="25" rows="2" id="rationale" class="text ui-widget-content ui-corner-all" />
	  </fieldset>
	</form>
</div>
